<?php defined('SYSPATH') or die('No direct script access.');

class Karma_Controller extends Controller
{

    public function index()
    {
    		$fromUser=$this->input->post('from','-');
    		$toUser=$this->input->post('to','-');
    		$fromUserConverted=($fromUser=='-')?null:$fromUser;
    		$toUserConverted=($toUser=='-')?null:$toUser;
    		$orderBy=$this->input->post('orderby','from');
    		$direction=$this->input->post('direction','asc');
    		$since=$this->input->post('since','-');
    		$sinceConverted=null;
    		switch ($since)
    		{
    			case '1d':$sinceConverted=60*60*24;break;
    			case '1w':$sinceConverted=60*60*24*7;break;
    			case '30d':$sinceConverted=60*60*24*30;break;
    		}
				$karmaModel=new Karma_Model();
				$fromUsers=$karmaModel->getFromUsers();
				$fromUsers['-']='--- All ---';
				$fromUsers['*']='--- SUM ---';
				$toUsers=$karmaModel->getToUsers();
				$toUsers['-']='--- All ---';
				$toUsers['*']='--- SUM ---';
				ksort($fromUsers,SORT_STRING);
				ksort($toUsers,SORT_STRING);
				if (($fromUser!='*') && ($toUser!='*')) $resultSet=$karmaModel->getKarmaDetailed($fromUserConverted,$toUserConverted,$sinceConverted,$orderBy,($direction=='desc'));
				if (($fromUser!='*') && ($toUser=='*')) $resultSet=$karmaModel->getKarmaGiven($fromUserConverted,$sinceConverted,$orderBy,($direction=='desc'));
				if (($fromUser=='*') && ($toUser!='*')) $resultSet=$karmaModel->getKarmaReceived($toUserConverted,$sinceConverted,$orderBy,($direction=='desc'));
				if (($fromUser=='*') && ($toUser=='*')) $resultSet=array();

				$karmaView = new View('karma_table');
				$karmaView->fromUserSelected=$fromUser;
				$karmaView->toUserSelected=$toUser;
				$karmaView->fromUsers=$fromUsers;
				$karmaView->toUsers=$toUsers;
				$karmaView->since=$since;
				$karmaView->orderBy=$orderBy;
				$karmaView->direction=$direction;
				$karmaView->resultSet=$resultSet;
        $karmaView->render(TRUE);
    }

}
